package com.itheima.controller.backend;

import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/checkgroup")
@Api(tags ="检查组相关接口")
public class CheckGroupController {

    @Autowired
    private CheckGroupService checkGroupService;
    /**
     * 新增检查组
     * @param group
     * @param checkitemIds
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增检查组")
    public Result add(@RequestBody CheckGroup group, @RequestParam List<Integer> checkitemIds){
        log.info("新增检查组：{},检查组中的检查项id：{}",group,checkitemIds);
        checkGroupService.add(group,checkitemIds);
        return new Result(true,"新增检查组成功");
    }

    /**
     * 根据检查组id删除检查组
     * @param id
     * @return
     */
    @GetMapping("/deleteById")
    @ApiOperation("根据检查组id删除检查组")
    public Result deleteById(@RequestParam Integer id){
        log.info("根据检查组id删除检查组：{}",id);
        checkGroupService.deleteById(id);
        return new Result(true,"删除检查组成功");
    }

    /**
     * 根据检查组id查询检查组
     * @param setmealId
     * @return
     */
    @GetMapping("/findCheckGroupIdsBySetmealId")
    @ApiOperation("根据套餐id查询关联的检查项id")
    public Result findCheckGroupIdsBySetmealId(@RequestParam Integer setmealId){
        log.info("根据套餐id查询关联的检查项id：{}",setmealId);
        List<Integer> checkItemIds = checkGroupService.findCheckGroupIdsBySetmealId(setmealId);
        return new Result(true,"查询检查组成功",checkItemIds);
    }

    @PostMapping("/findPage")
    @ApiOperation("分页查询检查项")
    public PageResult groupFindPage(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询检查项：{}", queryPageBean);
        PageResult pageResult = checkGroupService.findPage(queryPageBean);
        return pageResult;

    }

    /**
     * 编辑检查组
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @PostMapping("/edit")
    @ApiOperation("编辑检查组")
    public Result edit(@RequestBody CheckGroup checkGroup, @RequestParam List<Integer> checkitemIds){
        log.info("编辑检查组：{}",checkGroup);
        checkGroupService.edit(checkGroup,checkitemIds);
        return new Result(true,"编辑检查组成功");
    }

    @GetMapping("/findAll")
    @ApiOperation("查询所有检查组")
    public Result findAll(){
        log.info("查询所有检查组");
        List<CheckGroup> checkGroups = checkGroupService.findAll();
        return new Result(true,"查询所有检查组成功",checkGroups);
    }

    @GetMapping("/findById")
    @ApiOperation("根据检查组id查询检查组")
    public Result findById(@RequestParam Integer id){
        log.info("根据检查组id查询检查组：{}",id);
        CheckGroup checkGroup = checkGroupService.findById(id);
        return new Result(true,"查询检查组成功",checkGroup);
    }

}
